
/*
29 avr. 2012 jblanchard 
*/

package org.jcb.control.viewswing;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jcb.control.dao.data.ChineseDatabase;
import org.jcb.control.dto.ChineseQuestionnaire;
import org.jcb.control.dto.DataChineseLanguage;
import org.jcb.control.service.GenerateQuestionnaireRandomStrategy;

public class MainView implements ActionListener {
    JLabel labelQuestion = new JLabel("chinois");
    JLabel labelReponse1 = new JLabel("pinyin");
    JLabel labelReponse2 = new JLabel("français");
    JLabel Question = new JLabel("Hello World");
    JLabel Reponse1 = new JLabel("Rep1");
    JLabel Reponse2 = new JLabel("Rep2");
    JButton answerButton = new JButton("Answer");
    JButton nextButton = new JButton("Next");
    //ChineseQuestionnaire cq ;
    DataChineseLanguage dcl ;
    
    ChineseQuestionnaire cq ;

    public ChineseQuestionnaire getCq() {
		return cq;
	}

	public void setCq(ChineseQuestionnaire cq) {
		this.cq = cq;
	}

	public void setQuestion(String question) {
    	Question.setText(question);
    }
    
    public void setReponse(String rep1, String rep2) {
    	Reponse1.setText(rep1);
    	Reponse2.setText(rep2);
    }

    
	public  MainView () {
	 	JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6,1,5,5));
        frame.getContentPane().add(panel);	 
        //Add the ubiquitous "Hello World" label.
        Font f = new Font("Arial Unicode MS", Font.PLAIN, 24); 
        Question.setFont(f);
        Reponse1.setFont(f);
        Reponse2.setFont(f);
        
        frame.setSize(400, 400);
        panel.add(labelQuestion);
        panel.add(Question);
        panel.add(labelReponse1);
        panel.add(Reponse1);
        panel.add(labelReponse2);
        panel.add(Reponse2);
        
        nextButton.addActionListener(this);
        answerButton.addActionListener(this);
        
        nextButton.setBounds(50, 60, 80, 30);
//        nextButton.addActionListener(new ActionListener() {
//            public void actionPerformed(ActionEvent event) {
//                System.exit(0);
//           }
//        });	        
        panel.add(nextButton);
        panel.add(answerButton);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == nextButton) 
		{
			dcl = cq.nextQuestion();
			setQuestion("" + dcl.getChinese() + "");
			setReponse("", "");
		}
		else if (e.getSource() == answerButton) 
		{
			setReponse(dcl.getPinyin(), dcl.getTranslated());
//			if (cq.getExample(i).length() > 0) {
//				System.out.println(cq.getExample(i));
//			}		
		}
		
	}
	
	public void nextQuestion() {
		
	}
	
	
	public static void main(String[] args) throws InterruptedException {
		MainView mv = new MainView();
		mv.setQuestion("toto");
		
		List<DataChineseLanguage> data = ChineseDatabase.loadData();
		ChineseQuestionnaire cq = GenerateQuestionnaireRandomStrategy.generate(data, "1", 50);
		mv.setCq(cq);
		
		
//		for (int i = 0 ; i < cq.getNbQuestion() ; i++) {
//			System.out.println(cq.getChineseQuestion(i));
//			DataChineseLanguage dcl = cq.getQuestion(i);
//			mv.setQuestion("" + dcl.getChinese() + "");
//			mv.setReponse("", "");
//			Thread.sleep(2000);
//			System.out.println(cq.getPinyin(i) + " " + cq.getCarCompose(i));
//			mv.setReponse(dcl.getPinyin(), dcl.getTranslated());
//			if (cq.getExample(i).length() > 0) {
//				System.out.println(cq.getExample(i));
//			}
//			Thread.sleep(1000);
//		}
		
		
	}
	
	
}
